import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { extractUrls } from '../api/utils';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    
    {
      path: '/Hmoe',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    }
    ,
    {
      path: '/',
      name: 'blogin',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/B-loginView.vue')
    },
    {
      path: '/backend',
      name: 'backend',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/BackendView.vue'),
      children: [{
        path: '/roles',
      name: 'roles',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/RolesView.vue')
        },
        {
          path: '/guar',
        name: 'guar',
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('../views/GuaranteesView.vue')
          },
          {
            path: '/with',
          name: 'with',
          // route level code-splitting
          // this generates a separate chunk (About.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () => import('../views/WithView.vue')
            },
            {
              path: '/service',
            name: 'service',
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import('../views/SeviceView.vue')
              },
              {
                path: '/contract',
              name: 'contract',
              // route level code-splitting
              // this generates a separate chunk (About.[hash].js) for this route
              // which is lazy-loaded when the route is visited.
              component: () => import('../views/ContractView.vue')
                },
                {
                  path: '/order',
                name: 'order',
                // route level code-splitting
                // this generates a separate chunk (About.[hash].js) for this route
                // which is lazy-loaded when the route is visited.
                component: () => import('../views/OrderView.vue')
                  },
                {
                  path: '/roleadd',
                  name: 'roleadd',
                  // route level code-splitting
                  // this generates a separate chunk (About.[hash].js) for this route
                  // which is lazy-loaded when the route is visited.
                  component: () => import('../views/Roleadd.vue')
                },
                {
                  path: '/roledel',
                  name: 'roledel',
                  // route level code-splitting
                  // this generates a separate chunk (About.[hash].js) for this route
                  // which is lazy-loaded when the route is visited.
                  component: () => import('../views/Rolesdel.vue')
                },
                {
                  path: '/addworkflow',
                  name: 'addworkflow',
                  // route level code-splitting
                  // this generates a separate chunk (About.[hash].js) for this route
                  // which is lazy-loaded when the route is visited.
                  component: () => import('../views/AddWorkflow.vue')
                },
                {
                  path: '/workper',
                  name: 'workper',
                  // route level code-splitting
                  // this generates a separate chunk (About.[hash].js) for this route
                  // which is lazy-loaded when the route is visited.
                  component: () => import('../views/WorkPermissionView.vue')
                }]

    }]

})


// 递归函数提取URL
// function extractUrls(data) {
//   const urls = [];
//   data.forEach(item => {
//     if (item.url) {
//       urls.push(decodeURI(item.url)); // 解码并添加URL
//     }
//     if (item.son) {
//       urls.push(...extractUrls(item.son)); // 递归提取子项URL
//     }
//     if (item.son1) {
//       urls.push(...extractUrls(item.son1)); // 递归提取子项URL
//     }
//   });
//   return urls;
// }

// 从localStorage中获取menulist，并提取URL
const menulist = JSON.parse(localStorage.getItem('menulist') || '[]');
console.log(menulist);
const whiteList = extractUrls(menulist);
console.log(whiteList);
// 路由前置守卫
router.beforeEach((to, from, next) => {
  const reslist = ['/login', '/register',"/"]; // 白名单路径
  if (reslist.indexOf(to.path) == -1) {
    const token = localStorage.getItem('token');
    console.log(to.path);
    if (token) {
      // 验证是否在白名单中
      console.log(to.path);
      if (whiteList.indexOf(to.path) >= 0) {
        next();
      } else {
        alert("无权访问此页面");
        next({ name: 'blogin' });
      }
    } else {
      alert("请先登录");
      next({ name: 'blogin' });
    }
  } else {
    next();

  }
});
export default router

